<template>
  <page-header-wrapper>
    <a-card class="search-card  m-b-20" :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="14" :lg="12" :xl="8" :sm="24">
              <a-form-item label="参数名称">
                <a-input v-model="queryParam.Csmc" @pressEnter="$refs.table.refresh(true)" placeholder="请输入参数名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="14" :lg="12" :xl="8" :sm="24">
              <a-form-item label="key值">
                <a-input v-model="queryParam.ClassKey" @pressEnter="$refs.table.refresh(true)" placeholder="请输入key值"/>
              </a-form-item>
            </a-col>
            <a-col :md="10" :lg="8" :xl="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="handleReset">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card>
      <s-table
        class="parameter-table"
        ref="table"
        size="default"
        :columns="columns"
        :data="loadData"
        :rowKey="record => record.Id"
        :scroll="{x: true}"
        showPagination="auto"
        :pagination="{ showTotal: total => `共 ${total} 条` }"
      >
        <template slot="tabAction">
          <a-button type="primary" icon="plus" @click="handleOperation('add')">新增参数</a-button>
        </template>
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="status" slot-scope="text">
          <a-badge :status="text | statusTypeFilter" :text="text | statusFilter" />
        </span>
        <span slot="description" slot-scope="text">
          <ellipsis :length="4" tooltip>{{ text }}</ellipsis>
        </span>
        <span slot="up" slot-scope="record">
          <a-tag @click="handleUp(record.Id)"><a-icon type="arrow-up" /></a-tag>
        </span>
        <span slot="down" slot-scope="record">
          <a-tag @click="handelDown(record.Id)"><a-icon type="arrow-down" /></a-tag>
        </span>

        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleAddChild(record)">新增子参数</a>
            <a-divider type="vertical" />
            <a @click="handleOperation('update', record.Id)">修改</a>
            <a-divider type="vertical" />
            <a-popconfirm
              title="您确定是否删除当前参数?"
              ok-text="是"
              cancel-text="否"
              placement="bottomLeft"
              @confirm="handleRemove(record.Id)"
            >
              <a-icon slot="icon" type="question-circle-o" />
              <a href="#">删除</a>
            </a-popconfirm>
          </template>
        </span>
      </s-table>
    </a-card>
    <edit-parameter :visibles.sync="visible" :propsData="propsData" @refresh="handleRefresh"></edit-parameter>
    <edit-child v-if="visibleChild" :visibles.sync="visibleChild" :pid="pid" :name="names"></edit-child>
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getParameterList, delParameters, changeParameters } from '@/api/system'
import EditParameter from './edit'
import EditChild from './editChild'
const columns = [
  {
    title: '序号',
    align: 'left',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '参数名称',
    align: 'left',
    dataIndex: 'Csmc'
  },
  {
    title: '唯一key值',
    align: 'left',
    dataIndex: 'ClassKey'
  },
  {
    title: '备注',
    align: 'left',
    dataIndex: 'Ms',
    scopedSlots: { customRender: 'description' }
  },
  {
    title: '状态',
    align: 'left',
    dataIndex: 'Mode',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '排序',
    align: 'left',
    dataIndex: 'PX'
  },
  {
    title: '上移',
    align: 'left',
    width: 80,
    scopedSlots: { customRender: 'up' }
  },
  {
    title: '下移',
    align: 'left',
    width: 80,
    scopedSlots: { customRender: 'down' }
  },
  {
    title: '操作',
    align: 'center',
    dataIndex: 'action',
    fixed: 'right',
    width: 200,
    scopedSlots: { customRender: 'action' }
  }
]

const statusMap = {
  '0': {
    status: 'processing',
    text: '启用'
  },
  '-1': {
    status: 'default',
    text: '停用'
  }
}

export default {
  name: 'Parameter',
  components: {
    STable,
    Ellipsis,
    EditParameter,
    EditChild
  },
  data () {
    this.columns = columns
    return {
      visible: false,
      visibleChild: false,
      pid: '',
      names: '',
      propsData: {
        type: 'add',
        id: null
      },
      // 查询参数
      queryParam: {
        Fid: '0',
        ClassKey: '',
        Csmc: ''
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getParameterList(requestParameters).then(res => {
          return res
        })
      }
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type.toString()].text
    },
    statusTypeFilter (type) {
      return statusMap[type.toString()].status
    }
  },
  methods: {
    handleOperation (type, id) {
      this.visible = true
      this.propsData = { type, id }
    },
    handleAddChild (row) {
      this.visibleChild = true
      this.pid = row.Id
      this.names = row.Csmc
    },
    handleUp (ID) {
      changeParameters({ State: 'up', ParametersID: ID }).then(res => {
        if (res.code === 200) {
          this.$message.success('参数上移成功')
          this.$refs.table.refresh()
        } else {
          this.$message.error('参数上移失败')
        }
      }).catch((err) => {
        this.$message.error(err)
      })
    },
    handelDown (ID) {
      changeParameters({ State: 'down', ParametersID: ID }).then(res => {
        if (res.code === 200) {
          this.$message.success('参数下移成功')
          this.$refs.table.refresh()
        } else {
           this.$message.error('参数下移失败')
        }
      }).catch((err) => {
        this.$message.error(err)
      })
    },
    handleRemove (Id) {
      delParameters({ Id }).then((res) => {
        if (res.code === 200) {
          this.$refs.table.refresh()
          this.$message.success('参数删除成功', 2.5)
        } else {
          this.$message.error('参数删除失败, 请稍后重试', 2.5)
        }
      }).catch((err) => {
        this.$message.error(err, 2.5)
      })
    },
    handleRefresh () {
      this.$refs.table.refresh()
    },
    handleReset () {
      this.queryParam = {
        Fid: '0',
        ClassKey: '',
        Csmc: ''
      }
      this.$refs.table.refresh()
    }
  }
}
</script>
<style lang="less" scoped>
  .parameter-table {
    /deep/.ant-tag {
      cursor: pointer;
    }
  }
</style>
